//
//  SwipeTbViewNav.m
//  ZITHER
//
//  Created by weixin on 2018/11/9.
//  Copyright © 2018 weixn. All rights reserved.
//

#import "SwipeTbViewNav.h"
#import "SKUDetailTakeinVC.h"

@implementation SwipeTbViewNav


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self setUI];
    }
    return self;
}

-(void)setModel:(skuDetailModel *)model{
    _model = model;
    if ([model.traced isEqualToString:@"true"]) {
        self.btnLike.selected = YES;
    }else{
        self.btnLike.selected = NO;
    }
    
    if ([model.myEnquiryFlag isEqualToString:@"1"]) {
        self.btnMore.hidden = NO;
    }else if ([model.myEnquiryFlag isEqualToString:@"2"]){
        self.btnMore.hidden = NO;
    }else{
        self.btnMore.hidden = YES;
    }
    
//    self.btnLike.selected =
//    self.btnMore.selected =

}

-(void)setUI{
    [self addSubview:self.btnLike];
    [self addSubview:self.btnMore];
    BaseBtn * backBtn = [[BaseBtn alloc]initializeFrame:CGRectMake(3, NaviHeight-40, 40, 40) NormalImage:@"common_back" SeleImage:@""];
    backBtn.backgroundColor = [UIColor randomColor];
    @weakify(self);
    [[backBtn rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self);
        [self.getNav popViewControllerAnimated:YES];
    }];
    [self addSubview:backBtn];
    
    [self.btnLike mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(backBtn.mas_centerY).offset(0);
        make.right.equalTo(self.mas_right).offset(-5);
        make.width.height.mas_equalTo(40);
    }];
    
    [self.btnMore mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(backBtn.mas_centerY).offset(0);
        make.right.equalTo(self.btnLike.mas_left).offset(-5);
        make.width.height.mas_equalTo(40);
    }];
}
-(QMUIButton *)btnLike{
    if (!_btnLike) {
        _btnLike = [[QMUIButton alloc]initWithFrame:CGRectMake(kScreenW-60,NaviHeight-50,50,50)];
        [_btnLike setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_btnLike setImage:[UIImage imageNamed:@"nav_btn_collect"] forState:UIControlStateNormal];
        [_btnLike setImage:[UIImage imageNamed:@"nav_btn_collected"] forState:UIControlStateSelected];
        
        
        @weakify(self);
        [[_btnLike rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(__kindof UIControl * _Nullable x) {
            @strongify(self);
            
            self.btnLike.selected = !self.btnLike.selected;
            [self btnClick:0];
        }];
    }
    return _btnLike;
}
-(QMUIButton *)btnMore{
    if (!_btnMore) {
        _btnMore = [[QMUIButton alloc]initWithFrame:CGRectMake(kScreenW-100,NaviHeight-50,50,50)];
        [_btnMore setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_btnMore setImage:[UIImage imageNamed:@"nav_btn_more"] forState:UIControlStateNormal];
//        if (![self.model.myEnquiryFlag isEqualToString:@"1"]) {
//            _btnMore.selected = YES;
//        }
        
//        [self.NavView addSubview:_btnMore];
        @weakify(self);
        [[_btnMore rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(__kindof UIControl * _Nullable x) {
            @strongify(self);
            QMUIAlertAction *action1 = [QMUIAlertAction actionWithTitle:@"取消" style:QMUIAlertActionStyleCancel handler:^(__kindof QMUIAlertController *aAlertController, QMUIAlertAction *action) {
            }];
            
            QMUIAlertAction *action4 = [QMUIAlertAction actionWithTitle:@"终止询价" style:QMUIAlertActionStyleDefault handler:^(__kindof QMUIAlertController *aAlertController, QMUIAlertAction *action) {
                [self btnClick:1];

            }];
            
            QMUIAlertAction *ac2 = [QMUIAlertAction actionWithTitle:@"历史已接受报价" style:QMUIAlertActionStyleDefault handler:^(__kindof QMUIAlertController *aAlertController, QMUIAlertAction *action) {
                [self btnClick:2];

            }];
            
            
            QMUIAlertController *alertController = [QMUIAlertController alertControllerWithTitle:@"" message:@"" preferredStyle:QMUIAlertControllerStyleActionSheet];
            
            [alertController addAction:action1];
            
            [alertController addAction:ac2];
            
            if (![self.model.myEnquiryFlag isEqualToString:@"2"]) {
                [alertController addAction:action4];
            }
            
            
            [alertController showWithAnimated:YES];
        }];
    }
    return _btnMore;
}

/** 按钮点击事件 */
-(void)btnClick:(NSInteger)index
{
    /** index (0.喜欢 1.终止询价 2.查看历史询价记录) */
    if (index == 0) {
        
        if (!self.btnLike.selected) {
            [[serviceDefualt loadcancelFollowGoods:self.model.goodsId reponseObjctClass:nil]subscribeNext:^(ResponseData * x) {
                if (x.success) {
                    [kUSER_INFO_MANAGER MyList];
                    self.btnLike.selected =  NO;
                    [Tools showTips:@"取消追踪"];
                }else{
                    self.btnLike.selected = YES;
                    if (![x.errorInfo isEqualToString:@"未登录"]) {
                        [Tools showTips:x.errorInfo];
                    }else{
//                        [Tools pushSinge];
                    }
                }
            }];
        }else{
            [[serviceDefualt loadonFollowGoods:self.model.goodsId reponseObjctClass:nil]subscribeNext:^(ResponseData * x) {
                if (x.success) {
                    [kUSER_INFO_MANAGER MyList];
                    self.btnLike.selected = YES;
                    [Tools showTips:@"已追踪"];
                }else{
                    self.btnLike.selected = NO;
                    if (![x.errorInfo isEqualToString:@"未登录"]) {
                        [Tools showTips:x.errorInfo];
                    }else{
//                        [Tools pushSinge];
                    }
                }
            }];
        }
    }else if (index == 1){
        [[serviceDefualt loadendEnquiryPrice:self.model.userEnquiryId reponseObjctClass:nil] subscribeNext:^(ResponseData* x) {
            if (x.success) {
                /* MARK: ================= 刷新首页数据 =================*/
                //                [self setLoad];
                [Tools showTips:@"终止询价成功"];
                self.block();
            } else {
                [Tools showTips:x.errorInfo];
            }
        }];
    }else if (index == 2){
        SKUDetailTakeinVC * vc = [[SKUDetailTakeinVC alloc]init];
        vc.goodsId = self.model.goodsId;
        vc.index = 3;//设置历史询价
        vc.leb.text = @"历史记录";
        [self.getNav pushViewController:vc animated:YES];
    }

}
@end
